#include "Button.h"

int Button::BtnSize=25;

Button::Button():
	MineCnt(0),
	state(NOT_PUSH),
	color(BackColor)
{}

Button::Button(int cnt, int _state, COLORREF _color, int _size):
	MineCnt(cnt),
	state(_state),
	color(_color)
{}

void Button::Reset()
{
	MineCnt=0;
	state=NOT_PUSH;
	color=BackColor;
	BtnSize=25;
}

void Button::IsLClicked(HDC hdc, int i, int j, const POINT& pt)
{
	if (state==NOT_PUSH)
	{
		state=IS_PUSH;
		color=RGB(255, 255, 255);
	}
	DrawBtn(hdc, i, j, pt);
}

void Button::IsPushing(HDC hdc, int i, int j, const POINT& pt, bool _st)
{
	if (state==NOT_PUSH)
	{
		if (_st==1)color=RGB(255, 255, 255);
		else color=BackColor;
	}
	DrawBtn(hdc, i, j, pt);
}

void Button::DrawBtn(HDC hdc, int i,int j, const POINT& pt)
{
	RECT rect;
	rect.left=pt.x+j*25;
	rect.right=rect.left+25;
	rect.top=pt.y+i*25;
	rect.bottom=rect.top+25;

	HBRUSH hBrush=CreateSolidBrush(color);
	SelectObject(hdc, hBrush);
	Rectangle(hdc, rect.left, rect.top, rect.right,rect.bottom);

	if (state!=NOT_PUSH)
	{
		SetBkColor(hdc, color);
		switch (state)
		{
		case IS_PUSH:
			SetTextColor(hdc, RGB(0, 0, 0));
			char s[5];
			switch (MineCnt)
			{
			case 0:
			break;
			case 9:
				DrawText(hdc, TEXT("X"), 1, &rect,DT_CENTER|DT_SINGLELINE|DT_VCENTER);
			break;
			default:
				DrawText(hdc, s, sprintf(s, "%d", MineCnt), &rect,DT_CENTER|DT_SINGLELINE|DT_VCENTER);
			}
		break;

		case IS_FLAG:
			SetTextColor(hdc, RGB(255, 255, 255));
			DrawText(hdc, TEXT("F"), 1, &rect,DT_CENTER|DT_SINGLELINE|DT_VCENTER);
		break;

		case UNKNOWN:
			SetTextColor(hdc, RGB(255, 255, 255));
			DrawText(hdc, TEXT("?"), 1, &rect,DT_CENTER|DT_SINGLELINE|DT_VCENTER);
		break;
		}
	}
	DeleteObject(hBrush);
}

